<?php
/*
 * @Author: Terry
 * @Date: 2024-01-02 20:15:31
 * @LastEditors: Terry
 * @LastEditTime: 2024-09-23 18:15:13
 * @Description: mqtt人脸设备
 * @FilePath: \auxface_api\app\certification\command\FaceMqtt.php
 */

declare (strict_types = 1);

namespace app\certification\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;
use app\certification\extend\libs\HanMa;

class FaceMqtt extends Command{
    protected function configure()
    {
        // 指令配置
        $this->setname('sendPullData')
        ->setDescription('mqtt人脸设备');
    }
    protected function execute(Input $input, Output $output){

        $server   = '127.0.0.1';
        $port     = 1883;
        $clientId = 'phpSubscriber';
        $username = 'admin';
        $password = 'password';
        $clean_session = false;
        
        $connectionSettings = (new ConnectionSettings)
            ->setUsername($username)
            ->setPassword($password);
        //连接
        $mqtt =  $this->mqtt = new MqttClient($server, $port, $clientId);
        $mqtt->connect($connectionSettings, $clean_session);
        printf("client connected\n");
        try {
           //上下线通知
            $mqtt->subscribe('mqtt/face/basic', function ($topic, $message) {
                // printf("Received message on topic [%s]: %s\n", $topic, $message);
                $this->changeStatus($message);
            }, 0);
    
            $mqtt->subscribe('mqtt/face/heartbeat', function ($topic, $message) {
                // printf("Received message on topic [%s]: %s\n", $topic, $message);
                $this->heartbeat($message);
            }, 0);
            
            //考勤上传
            $mqtt->subscribe("mqtt/face/+/Rec", function ($topic, $message) {
                // printf("Received message on topic [%s]: %s\n", $topic, $message);
                // dump('rec');
                $this->checkIn($message);
            }, 0);
            //陌生人识别考勤上传
            $mqtt->subscribe("mqtt/face/+/Snap", function ($topic, $message) {
                // printf("Received message on topic [%s]: %s\n", $topic, $message);
                // dump('Snap');
                $this->snap($message);
            }, 0);     
            
            //人员增删查改回复
            $mqtt->subscribe("mqtt/face/+/Ack", function ($topic, $message) {
                // printf("Received message on topic [%s]: %s\n", $topic, $message);
                $this->ack($message);
            }, 0);
            
            $mqtt->loop(true);
        } catch (\Throwable $th) {
            // dump($th->getMessage());
        }
       
    }   
    
    public function createSdk($deviceId){
        $this->sdk = new HanMa($this->mqtt,$deviceId);
    }
    public function snap($data){
        $data = json_decode($data,true);
        unset($data['info']['pic']);
        $this->createSdk($data['info']['facesluiceId']);
        $this->sdk->snap($data['info']);
    }
    public function changeStatus($data){
        $data = json_decode($data,true);
        $this->createSdk($data['info']['facesluiceId']);
        $this->sdk->changeStatus($data);
    }
    
    public function checkIn($data){
       $data = json_decode($data,true);
       $this->createSdk($data['info']['facesluiceId']);
       $this->sdk->checkIn($data['info']);
    }
    public function ack($data){
        $data = json_decode($data,true);
        $this->createSdk($data['info']['facesluiceId']);
        $func = str_replace('-','',lcfirst($data['operator']));
        $this->sdk->ack($func,$data['info']);
    }
    public function heartbeat($data){
        // dump('heartbeat');
        $data = json_decode($data,true);
        $this->createSdk($data['info']['facesluiceId']);
        $this->sdk->machineQueue();
   
    }
    
}
